import _ from 'lodash';

import { USER_INFO_KEY } from './constant';

/**
 * 设置登录用户信息
 * @param { any } userInfo
 * @default {}
 */
export const setCurrentUser = (userInfo: any = {}) => {
  window.sessionStorage.setItem(USER_INFO_KEY, JSON.stringify(userInfo));
};

/**
 * 获取登录用户信息
 * @return { Record<string, any> } userInfo
 */
export const getCurrentUser = (): Record<string, any> => {
  return JSON.parse(window.sessionStorage.getItem(USER_INFO_KEY) || '{}');
};

/**
 * 删除登录用户信息
 */
export const removeCurrentUser = () => {
  window.sessionStorage.removeItem(USER_INFO_KEY);
};

/**
 * 判断有无登录用户
 * @return { boolean } true | false
 */
export const hasCurrentUser = (): boolean => {
  return !_.isEmpty(getCurrentUser());
};

/**
 * 获取登录用户id
 * @return { number } id
 */
export const getCurrentUserId = (): number => {
  return getCurrentUser().id;
};

/**
 * 获取登录用户名
 * @return { string } username
 */
export const getCurrentUserName = (): string => {
  return getCurrentUser().username;
};
